package com.zqq.user.ui.activity

import android.os.Bundle
import android.util.Log
import android.view.View
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.zqq.baselibrary.ext.enable
import com.zqq.baselibrary.ext.onClick
import com.zqq.baselibrary.ui.activity.BaseMvpActivity
import com.zqq.provider.PushProvider
import com.zqq.provider.common.ProviderConstant
import com.zqq.provider.router.RouterPath
import com.zqq.user.R
import com.zqq.user.injection.component.DaggerUserComponent
import com.zqq.user.injection.module.UserModule
import com.zqq.user.presenter.LoginPresenter
import com.zqq.user.presenter.view.LoginView
import com.zqq.user.service.bean.UserInfo
import com.zqq.user.utils.UserPrefsUtils
import kotlinx.android.synthetic.main.activity_login.*
import org.jetbrains.anko.startActivity
import org.jetbrains.anko.toast

/**
 * 登录界面
 * wc
 */
@Route(path = RouterPath.UserCenter.PATH_LOGIN)
class LoginActivity : BaseMvpActivity<LoginPresenter>(), LoginView, View.OnClickListener {

    //阿里ARoute传递数值
    @Autowired(name = ProviderConstant.KEY_ORDER_ID)
    @JvmField
    var sss: Int = 0

    //3.在使用的地方通过注入的方式获取到接口，并调用业务方法
    @Autowired(name = RouterPath.MessageCenter.PATH_MESSAGE_PUSH)
    @JvmField
    var mPushProvider: PushProvider? = null


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_login)

        var sss = intent.getIntExtra(ProviderConstant.KEY_ORDER_ID, 0)
        Log.e("key--", sss.toString())

        initView()
    }

    //初始化视图
    private fun initView() {
        //按钮是否可以点击
        mLoginBtn.enable(mMobileEt, { isBtnEnable() })
        mLoginBtn.enable(mPwdEt, { isBtnEnable() })

        mLoginBtn.onClick(this)
        mForgetPwdTv.onClick(this)  //忘记密码
        mHeaderBar.getRightView().onClick(this)//注册
    }


    /**
     * 实例化UserComponent
     */
    override fun injectComponent() {
        DaggerUserComponent.builder()
                //因为UserCom依赖于ActivityCom所以需要ActivityCom实例，且其在BaseMvpActivity已实例化
                .activityComponent(mActivityComponent)
                .userModule(UserModule())
                .build()
                .inject(this)
        mPresenter.mView = this
    }


    override fun onClick(v: View) {
        when (v.id) {
            //新注册
            R.id.mRightTv -> {
                //可以改成ARouter模式
                startActivity<RegisterActivity>()   //此处用的是anko包
            }
            //登录
            R.id.mLoginBtn -> {
                //这里在消息定向发送时-要用到pushId,而pushId是在消息模块过来的，
                //3.所以要在登录模块调用消息模块的接口来获取pushId(要利用ARouter)
                mPresenter.login(mMobileEt.text.toString(), mPwdEt.text.toString(),
                        mPushProvider?.getPushId() ?: "")
            }
            //忘记密码
            R.id.mForgetPwdTv -> {
//                startActivity<ForgetPwdActivity>()
                ARouter.getInstance()
                        .build(RouterPath.UserCenter.PATH_FORGET_PWD)
                        .navigation()
            }
        }
    }


    //判断按钮是否可用
    private fun isBtnEnable(): Boolean {
        return mMobileEt.text.isNullOrEmpty().not() &&
                mPwdEt.text.isNullOrEmpty().not()
    }

    /**-----------------接口返回---------------------*****/
    //登录回调
    override fun onLoginResult(result: UserInfo) {
        toast("登录成功")
        UserPrefsUtils.putUserInfo(result)
        finish()

        //ceshi
        startActivity<UserInfoActivity>()
    }


}
